<?php 
require_once "string.func.php";
//通过GD库做验证码
function verifyImg($type=1,$length=4){
	//创建画布
	$width=80;
	$height=40;
	$img=imagecreatetruecolor($width,$height);
	//颜色
	$white=imagecolorallocate($img,255,255,255);
	$black=imagecolorallocate($img,0,0,0);
	//填充画布
	imagefilledrectangle($img,0,0,$width,$height,$white);
	$sess_name="verify";
	$chars=buildRandomString($type,$length);
	$_SESSION[$sess_name]=$chars;
	$fontfiles = array ("arial.TTF");
	for($i=0;$i<$length;$i++){
		$size=mt_rand(14,18);
		$angle=mt_rand(-15,15);
		$x=5+$i*$size;
		$y=mt_rand(20,26);
		$fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
		$text=substr($chars,$i,1);
		imagettftext($img,$size,$angle,$x,$y,$black,$fontfile,$text);
	}
	//输出
	header("content-type:image/ipeg");
	imagejpeg($img);
	imagedestroy($img);
}

//生成缩略图
function thumb($filename,$destination=null,$dst_w=null,$dst_h=null,$isReservedSource=true,$scale=0.5){
	list($src_w,$src_h,$imagetype)=getimagesize($filename);
	if(is_null($dst_w)||is_null($dst_h)){
		$dst_w=ceil($src_w*$scale);
		$dst_h=ceil($src_h*$scale);
	}
	$mime=image_type_to_mime_type($imagetype);
	$createFun=str_replace("/", "createfrom", $mime);
	$outFun=str_replace("/", null, $mime);
	$src_image=$createFun($filename);
	$dst_image=imagecreatetruecolor($dst_w, $dst_h);
	imagecopyresampled($dst_image, $src_image, 0,0,0,0, $dst_w, $dst_h, $src_w, $src_h);
	if($destination&&!file_exists(dirname($destination))){
		mkdir(dirname($destination),0777,true);
	}
	$dstFilename=$destination==null?getUniName().".".getExt($filename):$destination;
	$outFun($dst_image,$dstFilename);
	imagedestroy($src_image);
	imagedestroy($dst_image);
	if(!$isReservedSource){
		unlink($filename);
	}
	return $dstFilename;
}
?>